﻿using System;
using Newtonsoft.Json;

namespace Communications.PropertySearch.Resources
{
    public sealed class EmptyStringToNullConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(int?).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            int result;
            if (int.TryParse(reader.Value.ToString(), out result))
            {
                return result;
            }
            return existingValue;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}
